//
//  LCCouponService.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/28.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCCouponService.h"


@implementation LCCouponService

//获取广告链接
// 1、首页：首页焦点图
// 2、任务：任务栏焦点图
// 3、个人中心：个人中心广告图1/个人中心广告图2
+(void)getAdvertWithAdvertname:(NSString *)name Block:(void(^)(BOOL isSuccess, NSArray <LCCouponAdvertModel *>* list))block {
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"Advertname"] = name;
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLAdvert) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSDictionary * field = result[@"field"];
            NSArray * list = [LCCouponAdvertModel mj_objectArrayWithKeyValuesArray:field[@"items"]];
            block(YES, list);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil);
    }];
}

//获取首页友情链接
+(void)getCouponHeadItemWithGroupname:(NSString *)groupname Block:(void(^)(BOOL isSuccess, NSArray <LCCouponLinkModel *>* list))block {
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"Groupname"] = groupname;
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLLinks) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray *list = [LCCouponLinkModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            block(YES, list);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil);
    }];
}



//获取淘客推广位/收藏夹列表
+(void)getTaoKeFavoritesListWithBlock:(void(^)(BOOL isSuccess, NSArray <LCTaoKeFavoritesModel *>* list))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObject:@"taobao.tbk.dg.material.recommend" forKey:@"method"];
    [param setObject:@"34312468" forKey:@"app_key"];
    [param setObject:@"2.0" forKey:@"v"];
    [param setObject:@"md5" forKey:@"sign_method"];
    [param setObject:@"json" forKey:@"format"];
    [param setObject:@"true" forKey:@"simplify"];
    [param setObject:@"114812950334" forKey:@"adzone_id"];
    [param setObject:@"31519" forKey:@"material_id"];
    [param setObject:@"1" forKey:@"page_no"];
    [param setObject:@"20" forKey:@"page_size"];
        
    param[@"timestamp"] = [NSString ns_dateConversionsTimeString:[NSDate date] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    param[@"sign"] = [LCCouponService getTaoKeSingWithParam:param];
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:TaoKeBaseUrl HttpMethod:KGet HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if (result[@"error_response"]) {
            block(NO, nil);
        } else {
            NSArray * resultList = result[@"result_list"];
            NSArray * favorites_list = [resultList firstObject][@"favorites_info"][@"favorites_list"];
            NSArray *list = [LCTaoKeFavoritesModel mj_objectArrayWithKeyValuesArray:favorites_list];
            
            block(YES, list);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil);
    }];
}

//获取淘客收藏夹商品列表
+(void)getTaoKeGoodsListWithPage:(NSInteger)page FavoritesId:(NSString *)favoritesId Block:(void(^)(BOOL isSuccess, NSArray <LCTaoKeGoodsModel *>* list, NSInteger total))block; {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObject:@"taobao.tbk.dg.material.recommend" forKey:@"method"];
    [param setObject:@"34312468" forKey:@"app_key"];
    [param setObject:@"2.0" forKey:@"v"];
    [param setObject:@"md5" forKey:@"sign_method"];
    [param setObject:@"json" forKey:@"format"];
    [param setObject:@"true" forKey:@"simplify"];
    [param setObject:@"114812950334" forKey:@"adzone_id"];
    [param setObject:@"31539" forKey:@"material_id"];
    [param setObject:favoritesId forKey:@"favorites_id"];
    [param setObject:kFORMAT(@"%ld",page) forKey:@"page_no"];
    [param setObject:@"20" forKey:@"page_size"];
        
    param[@"timestamp"] = [NSString ns_dateConversionsTimeString:[NSDate date] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    param[@"sign"] = [LCCouponService getTaoKeSingWithParam:param];
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:TaoKeBaseUrl HttpMethod:KGet HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if (result[@"error_response"]) {
            block(NO, nil, 0);
        } else {
            NSArray *list = [LCTaoKeGoodsModel mj_objectArrayWithKeyValuesArray:result[@"result_list"]];
            NSInteger total = [result[@"total_count"] integerValue];
            block(YES, list, total);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
}

+(NSString *)getTaoKeSingWithParam:(NSMutableDictionary *)param {
    NSArray * keyArr = param.allKeys;
    NSArray * sortedArr = [keyArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    NSString *query = @"";
    NSString * secret = @"bc56b9c1fee748e7ae03273ec1767fb2";
    query = [query stringByAppendingString:secret];
    for (NSString * key in sortedArr) {
        if ([LCFunction isEmptyString:key] && [LCFunction isEmptyString:param[key]]) {
            query = [query stringByAppendingFormat:@"%@%@",key,param[key]];
        }
    }
    query = [query stringByAppendingString:secret];
    
    NSString * sign = [LCFunction ns_md5:query];
    return sign.uppercaseString;
}


@end
